import { Request, Response, NextFunction } from 'express';
import _ from 'lodash';
import { getIndex, getIdIndex, corner, changeStatus } from './index.service';
import { getShop } from '../shop/shop.service';
import { getJobs } from '../jobs/jobs.service';
import { getCar } from '../car/car.service';
import { getMovice } from '../house/house.service';
import { getSwiper } from '../swiper/swiper.service';
import { getNotification } from '../notification/notification.service';
import { getAdvertise } from '../advertise/advertise.service';

/**
 * 获取首页内容
 */
export const index = async (
  request: Request,
  response: Response,
  next: NextFunction
) => {
  // const house = await getMovice(parseInt(`${status}`, 10), -1, -1, -1, -1, 6, 1, -1, -1, -1, -1, 2);
  // const action = await getMovice(parseInt(`${status}`, 10), -1, -1, -1, -1, 6, 1, -1, -1, -1, -1, 1);
  // const cartoon = await getMovice(parseInt(`${status}`, 10), -1, -1, -1, -1, 6, 1, -1, -1, -1, -1, 8);
  // const popular = await getMovice(parseInt(`${status}`, 10), -1, -1, -1, 1, 6, 1, 0, -1, -1, -1, -1);
  
  const [house, action, cartoon, recommend, drama, popular] = await Promise.all([
     getMovice(1, -1, -1, -1, -1, 6, 1, -1, -1, -1, -1, 2, ''),
     getMovice(1, -1, -1, -1, -1, 6, 1, -1, -1, -1, -1, 1, ''),
     getMovice(1, -1, -1, -1, -1, 6, 1, -1, -1, -1, -1, 8, ''),
     getMovice(1, -1, 1, -1, -1, 4, 1, 0, -1, -1, -1, -1, ''),
     getMovice(1, -1, -1, -1, -1, 6, 1, 0, -1, -1, -1, 6, ''),
     getMovice(1, -1, -1, -1, 1, 10, 1, 0, -1, -1, -1, -1, ''),
   ]);

  const swipers = await getSwiper(1, 10, 1, 1, 0);
  const notice = await getNotification(1);

  response.send({ house, action, cartoon, recommend, drama, popular, swipers, notice });
}

/**
 * 获取单集电影
 */
export const single = async (
  request: Request,
  response: Response,
  next: NextFunction
) => {
  
  const [fantaziye, action, newMovie, love, drama, horror] = await Promise.all([
     getMovice(1, 0, -1, -1, -1, 8, 1, -1, -1, -1, -1, 2, ''),
     getMovice(1, 0, -1, -1, -1, 8, 1, -1, -1, -1, -1, 1, ''),
     getMovice(1, 0, -1, -1, -1, 6, 1, -1, -1, -1, -1, -1, ''),
     getMovice(1, 0, -1, -1, -1, 8, 1, -1, -1, -1, -1, 5, ''),
     getMovice(1, 0, -1, -1, -1, 8, 1, -1, -1, -1, -1, 6, ''),
     getMovice(1, 0, -1, -1, -1, 8, 1, -1, -1, -1, -1, 20, ''),
   ]);

  const swipers = await getSwiper(1, 3, 1, 7, 0);

  response.send({ fantaziye, action, newMovie, love, drama, horror, swipers });
}

/**
 * 获取电视剧
 */
export const multiple = async (
  request: Request,
  response: Response,
  next: NextFunction
) => {

  const [china, usa, newMovie, korea, india, other] = await Promise.all([
    getMovice(1, 1, -1, 1, -1, 8, 1, -1, -1, -1, -1, -1, ''),
    getMovice(1, 1, -1, 3, -1, 8, 1, -1, -1, -1, -1, -1, ''),
    getMovice(1, 1, -1, -1, -1, 6, 1, -1, -1, -1, -1, -1, ''),
    getMovice(1, 1, -1, 13, -1, 8, 1, -1, -1, -1, -1, -1, ''),
    getMovice(1, 1, -1, 5, -1, 8, 1, -1, -1, -1, -1, -1, ''),
    getMovice(1, 1, -1, -1, -1, 8, 2, -1, -1, -1, -1, -1, ''),
  ]);

  const swipers = await getSwiper(1, 3, 1, -1, 1);

  response.send({ china, usa, newMovie, korea, india, other, swipers });
}

/**
 * 获取内容(按 ID 查找)
 */
export const indexDetail = async (
  request: Request,
  response: Response,
  next: NextFunction
) => {
  // 获取内容 ID
  const { postId } = request.params;

  // 调取内容
  try {
    const data = await getIdIndex(parseInt(postId, 10));

    // 做出响应
    response.send(data);
  } catch (error) {
    next(error);
  }
};

/**
 * 获取信息圈内容
 */
export const getCorners = async (
  request: Request,
  response: Response,
  next: NextFunction
) => {
  try {

    /**
     * 如果值为true，则获取当前用户所发布的内容
     * 如果值为false，则获取所有满足其他条件的数据
     */
    let getMy = request.query.getMy;
    let status = request.query.status;
    let pageNo = request.query.pageNo;
    let pageSize = request.query.pageSize;

    let user = request.user;
    let userId = user && getMy ? user.id : 0;

    if (getMy && !user) {
      response.send({data:{}});
    }else{
      const data = await corner(userId, parseInt(`${status}`, 10),parseInt(`${pageNo}`, 10),parseInt(`${pageSize}`, 10))
      // 做出响应
      response.send(data);
    }
  } catch (error) {
    next(error);
  }
}

/**
 * 更改信息圈内容状态
 */
export const changeCornersStatus = async (
  request: Request,
  response: Response,
  next: NextFunction
) => {
  // 获取状态、内容 ID、类
  let target_id = request.body.target_id;
  let type = request.body.type;
  let post = _.pick(request.body, ['status']);

  try {
    const data = await changeStatus(post, parseInt(`${type}`, 10), parseInt(`${target_id}`, 10))
    
    // 做出响应
    response.send(data);
  } catch (error) {
    next(error);
  }
}